import pygame
import barlines
import math

class Player(object):
    def __init__(self,bar_line):
        super(Player,self).__init__()
        self.health = 100
        self.sprite_sheet = pygame.image.load("../media/images/Player1.png").convert_alpha()
        self.temp_image = pygame.Surface((150,300),pygame.SRCALPHA)
        self.img = pygame.Surface((100,200),pygame.SRCALPHA)
        self.rect = self.img.get_rect()
        self.rect.centerx = 100
        self.rect.centery = bar_line.locY
        self.disp = 0
        self.move = 0
        self.score = 0
        self.bar = bar_line
        self.beats = 5
        self.frame=0
    def update(self,screen_rect):
        future_rect = self.rect.move(0,self.move)
        if future_rect.top < (self.bar.locY -400) or future_rect.bottom > self.bar.locY+200:
            self.disp = 0
        else:
            self.disp = self.move
        self.rect.move_ip(0,self.disp)
        self.move = 0
        if self.frame!=0:
            self.frame+=1
            if self.frame >= 16:
                self.frame=0
    def draw(self,screen):
        self.img=pygame.transform.scale(self.sprite_sheet,(600,300))
        screen.blit(self.img,self.rect,pygame.Rect(((self.frame)%8)*75,(self.frame/8+1)%2*150,75,150))
    def reset(self):
        self.x_move = 0
        self.y_move = 0
    def take_damage(self,amount):
        if ( self.health - amount ) < 0:
            self.health = 0
        elif (self.health - amount ) > 100:
            self.health = 100
        else:
            self.health -= amount
    def add_score(self,amount):
        if ( self.score + amount ) < 0:
            self.score = 0
        else:
            self.score += amount
    def beat(self,amount):
        if ( self.beats - amount ) < 0:
            self.beats = 0
        elif (self.beats - amount ) > 100:
            self.beats = 100
        else:
            self.beats -= amount
    def throw(self):
        self.frame=1